package cn.edu.ctbu.service.impl;

import cn.edu.ctbu.DTO.SetmealDetailDTO;
import cn.edu.ctbu.VO.DishDetailVO;
import cn.edu.ctbu.VO.PageDishVO;
import cn.edu.ctbu.VO.PageSetmealVO;
import cn.edu.ctbu.VO.SetmealDetailVO;
import cn.edu.ctbu.context.BaseContext;
import cn.edu.ctbu.entity.*;
import cn.edu.ctbu.mapper.SetmealMapper;
import cn.edu.ctbu.result.PageResult;
import cn.edu.ctbu.service.ICategoryService;
import cn.edu.ctbu.service.IDishService;
import cn.edu.ctbu.service.ISetmealDishService;
import cn.edu.ctbu.service.ISetmealService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 套餐 服务实现类
 * </p>
 *
 * @author author
 * @since 2024-05-12
 */
@Service
public class SetmealServiceImpl extends ServiceImpl<SetmealMapper, Setmeal> implements ISetmealService {

    @Autowired
    ICategoryService iCategoryService;
    @Autowired
    ISetmealDishService iSetmealDishService;
    @Autowired
    IDishService iDishService;
    @Override
    public PageResult get(Integer page, Integer pageSize, String name, Long categoryId, Integer status) {
        Page pagewrapper=new Page<>();
        if (page!=null && pageSize!=null) {
            pagewrapper.setCurrent(page).setSize(pageSize);
        }
        QueryWrapper wrapper=new QueryWrapper<>();
        if(name!=null){
            wrapper.like("name",name);
        }
        if(categoryId!=null){
            wrapper.eq("category_id",categoryId);
        }
        if(status!=null){
            wrapper.eq("status",status);
        }
        Page paged = page(pagewrapper, wrapper);
        List<Setmeal> setmeals=paged.getRecords();
        List<PageSetmealVO> list=new ArrayList<>();
        for (Setmeal setmeal:setmeals){
            PageSetmealVO vo=new PageSetmealVO();
            BeanUtils.copyProperties(setmeal,vo);
            Category byId = iCategoryService.getById(setmeal.getCategoryId());
            vo.setCategoryName(byId.getName());
            list.add(vo);
        }
        PageResult pageResult=PageResult.builder().total(paged.getTotal()).records(list).build();
        return pageResult;
    }

    @Override
    public void status(Long id) {
        Setmeal byId = getById(id);
        Integer status = byId.getStatus();
        Setmeal setmeal=new Setmeal();
        setmeal.setId(id);
        if(status==1){
            setmeal.setStatus(0);
        }else{
            setmeal.setStatus(1);
        }
        Long currentId = BaseContext.getCurrentId();
        setmeal.setUpdateUser(currentId);
        setmeal.setUpdateTime(LocalDateTime.now());
        updateById(setmeal);
    }

    @Override
    public void del(List<Long> ids) {
        for (Long id:ids){
            removeById(id);
            QueryWrapper wrapper=new QueryWrapper<>().eq("setmeal_id",id);
            iSetmealDishService.remove(wrapper);
        }
    }

    @Override
    public SetmealDetailVO getSetmealDetail(Long id) {
        //根据ID获得菜品口味信息
        Setmeal setmeal=getById(id);
        SetmealDetailVO vo=new SetmealDetailVO();
        BeanUtils.copyProperties(setmeal,vo);
        QueryWrapper wrapper=new QueryWrapper<>().eq("setmeal_id",id);
        List<SetmealDish> setmealdishes = iSetmealDishService.list(wrapper);

        //判断是否有口味选择
        if(setmealdishes==null){
            return vo;
        }
        vo.setDishes(setmealdishes);
        return vo;
    }

    @Override
    public void add(SetmealDetailDTO dto) {
        Setmeal setmeal=new Setmeal();
        BeanUtils.copyProperties(dto,setmeal);
        setmeal.setCreateTime(LocalDateTime.now()).setUpdateTime(LocalDateTime.now())
                .setCreateUser(BaseContext.getCurrentId()).setUpdateUser(BaseContext.getCurrentId());
        save(setmeal);
        for (SetmealDish setmealDish:dto.getDishes()){
            setmealDish.setSetmealId(setmeal.getId());
            iSetmealDishService.save(setmealDish);
        }
    }

    @Override
    public void put(SetmealDetailDTO dto) {
        Setmeal setmeal=new Setmeal();
        BeanUtils.copyProperties(dto,setmeal);
        setmeal.setUpdateTime(LocalDateTime.now())
               .setUpdateUser(BaseContext.getCurrentId());
        updateById(setmeal);
        //将原先的口味删除
        QueryWrapper wrapper=new QueryWrapper<>().eq("setmeal_id",dto.getId());
        iSetmealDishService.remove(wrapper);
        for (SetmealDish setmealDish:dto.getDishes()){
            setmealDish.setSetmealId(setmeal.getId());
            iSetmealDishService.save(setmealDish);
        }
    }

    @Override
    public List<SetmealDetailVO> getSetmeal() {
        QueryWrapper wrapper=new QueryWrapper<>().eq("status",1);
        List<Setmeal> list = list(wrapper);
        List<SetmealDetailVO> vo=new ArrayList<>();
        for (Setmeal setmeal:list){
            Long id=setmeal.getId();
            SetmealDetailVO detailVO = getSetmealDetail(id);
            vo.add(detailVO);
        }

        return vo;
    }

}
